package A_JDBC;

import org.junit.Test;

import java.sql.*;

/*
  Statement对象中处理sql语句的方法:
    boolean execute(String sql);
      用于DDL DML
    String executeQuery(String sql);
      用于DQL
    int executeUpdate(String sql);
      用于DML

 */

public class JDBCDemo2 {
    @Test
    public void Test01DDL() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql:///demo",
                "root",
                "123456"
        );
        Statement sta = conn.createStatement();
        String sql1 ="DROP TABLE IF EXISTS u111";
        String sql2 ="create table u111(id int primary key,name varchar(20))";
        String sql3 ="insert into u111 values(1,'lala')";
        String sql4 ="select * from u111";
        String sql5 ="insert into u111 values(2,'kaka')";
        String sql6 ="select * from u111";

        sta.execute(sql1);
        sta.execute(sql2);
        sta.execute(sql3);
        //可以执行
        boolean execute = sta.execute(sql4);
        System.out.println(execute);
        //有返回值
        ResultSet result1 = sta.executeQuery(sql4);
        System.out.println(result1);  //则返回 true
        // 影响的行数
        int i = sta.executeUpdate(sql5);
        System.out.println(i);

        ResultSet result2 = sta.executeQuery(sql6);
        //处理结果集
        while (result2.next()){
            int id = result2.getInt(1);
            String name = result2.getString(2);
            System.out.println(id+"号: "+name);
        }
        sta.close();
        conn.close();
    }
}
